/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.grt192.controller.breakaway.teleop;

import com.grt192.core.GRTRobot;
import com.grt192.core.StepController;
import com.grt192.event.GlobalEvent;
import com.grt192.event.GlobalListener;
import com.grt192.event.SensorEvent;
import com.grt192.event.component.ButtonListener;
import com.grt192.event.component.JoystickEvent;
import com.grt192.event.component.JoystickListener;
import com.grt192.mechanism.GRTDriverStation;
import com.grt192.mechanism.breakaway.Roller;
import com.grt192.sensor.GRTJoystick;

/**
 * 
 * @author Vikram
 */
public class RollerController extends StepController implements ButtonListener {

	public static final double DELAY_TIME = 1000; // delay after kicking before
	// rollers turn back on
	public static final double MAX_SPEED = 1;
	public static final double MIN_SPEED = 0;
	public static final double PUSH_SPEED = -1;

	private boolean enabled = true;
	private double currentSpeed = 0;
	private boolean kicking = false;
	private boolean pushing = false;
	private Roller rollers;
	private int count = 0;

	public RollerController(GRTDriverStation dStation, Roller rollers) {
		super();
		addMechanism("DriverStation", dStation);
		addMechanism("Rollers", rollers);
		this.rollers = (Roller) getMechanism("Rollers");
		((GRTJoystick) dStation.getSensor("leftJoystick"))
				.addButtonListener(this);
		((GRTJoystick) dStation.getSensor("rightJoystick"))
				.addButtonListener(this);
		rollers.grab(MAX_SPEED);
	}

	public void act() {
		if (kicking) {
			rollers.stop();
			if (count * SLEEP_INTERVAL >= DELAY_TIME) {
				kicking = false;
				count = 0;
				if (pushing) {
					rollers.push();
				} else {
					rollers.grab(currentSpeed);
				}

				// Will accelerate back to targetSpeed if button is still
				// pressed
				GRTRobot.getInstance().putGlobal("Kicking", new Boolean(false));
				System.out.println("Rollers enabled");
			} else
				count++;
		}

	}

	public void buttonUp(SensorEvent e, String key) {
		String joyStick = e.getSource().getId();
		if (key.equals("Button1") && joyStick.equals("right")) {
			currentSpeed = MIN_SPEED;
			if (enabled && !kicking && !pushing) {
				rollers.grab(currentSpeed);
			}
		}
		if (key.equals("Button1") && joyStick.equals("left")) {
			pushing = false;
			if (enabled && !kicking) {
				rollers.stop();
				rollers.grab(currentSpeed);
			}
		}
	}

	public void buttonDown(SensorEvent e, String key) {
		String joyStick = e.getSource().getId();
		if (key.equals("Button1") && joyStick.equals("right")) {
			currentSpeed = MAX_SPEED;
			if (enabled && !kicking && !pushing) {
				rollers.grab(currentSpeed);
			}
		}
		if (key.equals("Button1") && joyStick.equals("left")) {
			pushing = true;
			if (enabled && !kicking) {
				rollers.stop();
				rollers.push();
			}
		}
		if (key.equals("Button10") && joyStick.equals("right")) {
			enabled = !enabled;
			if (!enabled) {
				rollers.stop();
			} else {
				if (pushing) {
					rollers.push();
				} else {
					rollers.grab(currentSpeed);
				}
			}
		}

	}

}
